$(function () {
    //新增账户
    $("[name='btn_save_new_staff']").click(function () {
        var form = window.form_new_staff;
        var account = form.account.value;
        var mobile = form.mobile.value;
        var pwd = form.pwd.value;
        var repwd = form.repwd.value;
        var state = form.state.value;
        if (pwd != repwd) {
            bootbox.balert("确认密码输入有误");
            return;
        }

        var staff = {account: account, mobile: mobile, pwd: pwd, state: state};

        $.postJsonBody("/staff", staff, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_staff").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });


    //编辑账户
    $("[name='btn_save_edit_staff']").click(function () {
        var form = window.form_edit_staff;
        var id = form.id.value;
        var account = form.account.value;
        var mobile = form.mobile.value;
        /*var pwd = form.pwd.value;
        var repwd = form.repwd.value;*/
        var state = form.state.value;
        /*if (pwd != repwd) {
            bootbox.balert("确认密码输入有误");
            return;
        }*/

        var staff = {id: id, account: account, mobile: mobile, /*pwd: pwd,*/ state: state};
        $.putJsonBody("/staff/" + id, staff, function (re) {
            // tipper.success("已更新，请刷新页面");
            // $("#md_edit_staff").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    //删除账户
    $("[name='btn_delete_staff']").click(function () {
        var staffId = $(this).data("staff-id");
        bootbox.bconfirm("确定要删除这个账户吗？", function (re) {
            if (re) {
                $.delete("/staff/" + staffId, function (re) {
                    if (re.data) {
                        // tipper.success("删除成功，请刷新页面");
                    	window.location.href=window.location.href; 
                    	window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });


    // 账户详情
    $('#md_edit_staff').on('hide.bs.modal', function (event) {
        var form = window.form_edit_staff;
        form.reset();
    });
    $('#md_edit_staff').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("staff-id");
        var modal = $(this);
        var form = window.form_edit_staff;
        $.get("/staff/" + id, function (re) {
            var staff = re.data;
            form.id.value = staff.id;
            form.account.value = staff.account;
            form.mobile.value = staff.mobile;
            /*form.pwd.value = staff.pwd;
            form.repwd.value = staff.pwd;*/
            $(form.state).val(staff.state);
        });
    });
    
    
  //数据导出
    $("[name='btn_export']").click(function () {
        var keyword = $("[name='keyword']").val();
        var state = $("[name='state']").val();
       
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
        	bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                	 var url="/staff/export?keyword="+keyword+"&state="+state;
                     window.open(url);
                }
            });
        }else{
        	bootbox.balert("没有数据，无法导出!");
        }
    });
    
    //重置密码
    $("[name='btn_reset_pwd']").click(function () {
    	var staffId = $(this).data("staff-id") ;
    	var companyId = $(this).data("staff-companyid");
    	bootbox.bconfirm("确定要重置密码吗？", function (re) {
    		if(re){
    			$.get("/staff/resetPwd/" + staffId + "/" + companyId, function (re) {
                    if(re.data){
                   	 bootbox.balert("重置密码成功! 新密码为：qrcode123456");
                    }else{
                   	 bootbox.balert("重置密码失败!");
                    }
                });
    		}
    		
        });
    	 
    });
    
});

